<?php
/* DB Migration #1 : add 'open' to articles.state Set to be able to gather stats */
$res = mysql_query("DESCRIBE articles", $link)
	or die("Failed DESCRIBE articles: " . mysql_error() . "\n");
while ($row = mysql_fetch_array($res)) {
	if ($row['Field'] == 'state') {
		if (strstr($row['Type'], 'open') === FALSE) {
			mysql_query("ALTER TABLE articles CHANGE state state set('new','starred','unread','open') collate utf8_unicode_ci default 'new'", $link);
			if (mysql_errno($link) !== 0) {
				// Try for MySQL < 4.1
				mysql_query("ALTER TABLE articles CHANGE state state set('new','starred','unread','open') default 'new'", $link)
					or die("Failed ALTER TABLE articles for DB Migration #1: " . mysql_error() . "\n");
			}
		}
	}
}

/* DB Migration #2: Add subscriptions post processing table */
mysql_query("CREATE TABLE IF NOT EXISTS subs_post_processing (subId SMALLINT(5) UNSIGNED NOT NULL, rank SMALLINT(5) UNSIGNED NOT NULL, from_re TEXT NOT NULL collate utf8_unicode_ci, to_re TEXT NOT NULL collate utf8_unicode_ci) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci", $link);
if (mysql_errno($link) !== 0) {
	// Try for MySQL < 4.1
	mysql_query("CREATE TABLE IF NOT EXISTS subs_post_processing (subId SMALLINT(5) UNSIGNED NOT NULL, rank SMALLINT(5) UNSIGNED NOT NULL, from_re TEXT NOT NULL, to_re TEXT NOT NULL) ENGINE=MyISAM", $link)
		or die("Failed CREATE TABLE subs_post_processing for DB Migration #2: " . mysql_error() . "\n");
}

/* DB Migration #3: Add articles filters table */
mysql_query("CREATE TABLE IF NOT EXISTS filters (id SMALLINT(5) UNSIGNED NOT NULL, value TEXT NOT NULL, PRIMARY KEY (id)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci", $link);
if (mysql_errno($link) !== 0) {
	// Try for MySQL < 4.1
	mysql_query("CREATE TABLE IF NOT EXISTS filters (id SMALLINT(5) UNSIGNED NOT NULL, value TEXT NOT NULL, PRIMARY KEY (id)) ENGINE=MyISAM", $link)
		or die("Failed CREATE TABLE filters for DB Migration #3: " . mysql_error() . "\n");
}


/* DB Migration #4: add last_updated and error columns to subscriptions table */
$res = mysql_query("DESCRIBE subscriptions", $link)
	or die("Failed DESCRIBE subscriptions: " . mysql_error() . "\n");
$need_update = true;
while ($row = mysql_fetch_array($res)) {
	if ($row['Field'] == 'last_updated' || $row['Field'] == 'error') {
		$need_update = false;
		break;
	}
}
if ($need_update) {
	mysql_query("ALTER TABLE subscriptions DROP INDEX url", $link)
		or die("Failed DROP INDEX url for DB Migration #4: " . mysql_error() . "\n");
	mysql_query("ALTER TABLE subscriptions ADD last_updated DATETIME, ADD error TEXT", $link)
		or die("Failed ADD last_updated & error columns for DB Migration #4: " . mysql_error() . "\n");
	mysql_query("ALTER TABLE subscriptions ADD UNIQUE KEY url (url(256))", $link);
	if (mysql_errno($link) !== 0) {
		// Try for MySQL < 4.1
		mysql_query("ALTER TABLE subscriptions ADD UNIQUE KEY url (url(255))", $link)
			or die("Failed ADD UNIQUE KEY url for DB Migration #4: " . mysql_error() . "\n");
	}
}
?>